﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class NhanVien_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public NhanVien_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<NhanVien> getList_WithPassword_By(string whereCom, string orderCom)
        {
            List<NhanVien> data = new List<NhanVien>();
            string comStr = "Select * from [NhanVien]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        NhanVien obj = new NhanVien();

                        obj.UserName = dap.DaReader["UserName"].ToString();
                        obj.Password = dap.DaReader["Password"].ToString();
                        obj.Name = dap.DaReader["Name"].ToString();                        
                        obj.Permission = Convert.ToInt32(dap.DaReader["Permission"]);                        

                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<NhanVien> getList_WithPassword_ByKey()
        {
            return getList_WithPassword_By("", " [UserName] asc");
        }

        private UserAdmin getBy(string whereCom, string orderCom)
        {
            UserAdmin data = null;
            string comStr = "Select * from [NhanVien]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    data = new UserAdmin();
                    dap.DaReader.Read();

                    data.UserName = dap.DaReader["UserName"].ToString();                    
                    data.Name = dap.DaReader["Name"].ToString();
                    data.Permission = Convert.ToInt32(dap.DaReader["Permission"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        private UserAdmin getBy(string whereCom, string orderCom, List<SqlParameter> dataPrama)
        {
            UserAdmin data = null;
            string comStr = "Select * from [NhanVien]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                
                //this.hasData = dap.HasData;
                if (dataPrama != null)
                {
                    dap.AddParameters(dataPrama);
                }
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                if (dap.HasData)
                {
                    data = new UserAdmin();
                    dap.DaReader.Read();

                    data.UserName = dap.DaReader["UserName"].ToString();
                    data.Name = dap.DaReader["Name"].ToString();
                    data.Permission = Convert.ToInt32(dap.DaReader["Permission"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public UserAdmin loginByKey(string UserName, string Password)
        {
            List<SqlParameter> dataPrama = new List<SqlParameter>();
            dataPrama.Add(
                new SqlParameter("@UserName", System.Data.SqlDbType.NVarChar)
                {
                    Value = UserName
                });
            dataPrama.Add(
                new SqlParameter("@Password", System.Data.SqlDbType.NVarChar)
                {
                    Value = Password
                });

            return getBy(" [UserName]=@UserName and [Password]=@Password ", "", dataPrama);
        }
    }
}